<!DOCTYPE html>
<!--

// Copyright 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

-->
<html>
<head>
<title></title>
<script src="../../../third_party/closure-library/closure/goog/base.js"></script>
<script>
// NOTE: base.js and all goog.require statements must be in JS script blocks
// NOTE: because they use document.write to add new script tags in place
goog.require('goog.testing.jsunit');
</script>
<script src="../../../src/traceur.js"></script>
</head>
<body>
<script>

var TreeWriter = traceur.outputgeneration.TreeWriter;
var SourceMapConsumer = traceur.outputgeneration.SourceMapConsumer;
var SourceMapGenerator = traceur.outputgeneration.SourceMapGenerator;

var errorReporter = {
  reportError: function(position, message) {
    console.error(message + ', ' + position);
  }
};

function parse(name, source) {
  var sourceFile = new traceur.syntax.SourceFile(name, source);
  var scanner = new traceur.syntax.Scanner(errorReporter, sourceFile);
  var parser = new traceur.syntax.Parser(errorReporter, scanner);
  console.log('Parsing', name);

  var tree = parser.parseProgram();
  return tree;
}

function testSourceMap() {
  
  function dumpLine(line, number) { 
      console.log( (number +1 ) + ': ' + line);
  }

  var src = 'function foo() { return 5; }\nvar \nf\n=\n5\n;\n';
  
  console.log('original:');
  src.split('\n').forEach(dumpLine);

  var filename = 'sourceMapThis.js';
  var tree = parse(filename, src);
  
  var testcases = [
    {generated: {line: 2, column: 0}, original: {line: 1, column: 0}},
    {generated: {line: 3, column: 0}, original: {line: 1, column: 17}},
    {generated: {line: 4, column: 0}, original: {line: 1, column: 24}},
    {generated: {line: 5, column: 0}, original: {line: 3, column: 0}},
    {generated: {line: 6, column: 0}, original: {line: 6, column: 0}}
  ];
  
  var generator = new SourceMapGenerator({file:filename});
  var options = {sourceMapGenerator: generator , showLineNumbers: true};
  var actual = TreeWriter.write(tree, options);
  
  console.log('generated:');
  actual.split('\n').forEach(dumpLine);
  
  var consumer = new SourceMapConsumer(options.sourceMap);
  
  testcases.forEach(function(testcase, caseNumber) {
    var actual = consumer.originalPositionFor(testcase.generated);
    var shouldBeTrue = actual.line === testcase.original.line;
    assert(caseNumber +' Line mismatch '+actual.line, shouldBeTrue); 
    shouldBeTrue = actual.column === testcase.original.column;
    assert(caseNumber +' Column mismatch '+actual.column, shouldBeTrue); 
  });
}
</script>
</body>
</html>
